const {Controller} = require("ee-core");
const Services = require("ee-core/services");
const {json} = require("../utils");

class SshController extends Controller {
    ssh = Services.get('ssh')

    constructor(ctx) {
        super(ctx);
    }


    async uploadFileByFtp(args) {
        try {
            this.ssh.uploadFileByFtp(args)
            return json({})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }

    }

    async unlinkFiles(args) {
        try {
            await this.ssh.unlinkFiles(args)
            return json({})
        } catch (e) {

            return json({code: 500, msg: e.message})
        }

    }

    async downloadFiles(args) {
        try {
            this.ssh.downloadFiles(args)
            return json({})
        } catch (e) {

            return json({code: 500, msg: e.message})
        }

    }

    async getItemById(args) {
        try {
            let data = await this.ssh.getItemById(args)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }

    }


    /**
     * 获取远程目录
     *
     */
    async sftpGetTree(args) {
        console.log(args)
        try {
            let data = await this.ssh.sftpGetTree(args)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e.message})
        }

    }

    /**
     * 链接ssh
     *
     */
    async connSsh(args) {
        try {
            return await this.ssh.connSsh(args)
        } catch (e) {
            return e
        }

    }

    /**
     * 链接ssh
     *
     */
    async createWindow(args) {
        try {
            await this.ssh.createWindow(args)
            return json({})
        } catch (e) {
            return e
        }

    }

    /**
     * 删除关闭
     *
     */
    async closeSSh(args) {
        try {
            return await this.ssh.closeSSh(args)
        } catch (e) {
            return e
        }

    }

    /**
     * 链接ssh
     *
     */
    async sendData(args) {
        console.log(args)
        return await this.ssh.sendData(args)
    }

    async getSsh(args) {
        console.log(args)
        try {
            return await this.ssh.getSsh(args)
        } catch (e) {
            return json({code: 500, msg: e})
        }
    }

    async delSsh(args) {
        console.log(args)
        try {
            await this.ssh.delSsh(args)
            return json({})
        } catch (e) {
            return json({code: 500, msg: e})
        }
    }

    async createOrUpdate(args) {
        console.log(args)
        try {
            await this.ssh.createOrUpdate(args)
            return json({})
        } catch (e) {
            return json({code: 500, msg: e})
        }
    }

    async previewFile(args) {
        try {
            let data = await this.ssh.previewFile(args)
            return json({data})
        } catch (e) {
            return json({code: 500, msg: e})
        }
    }

    async returnVideoStream(args) {
        try {
            console.log(args)
            return await this.ssh.returnVideoStream(args)

        } catch (e) {
            console.log(e)
            return json({code: 500, msg: e})
        }
    }

    async saveFileText(args) {
        try {
            await this.ssh.saveFileText(args)
            return json({})
        } catch (e) {
            console.log(e)
            return json({code: 500, msg: e})
        }
    }

    async updateFileName(args) {
        try {
            await this.ssh.updateFileName(args)
            return json({})
        } catch (e) {
            console.log(e)
            return json({code: 500, msg: e})
        }
    }
}

SshController.toString = () => '[class SshController]';
module.exports = SshController;